package com.libre.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.libre.auth.entity.Depart;
import com.libre.auth.service.DepartService;
import com.libre.common.base.BaseController;
import com.libre.common.enums.ResultEnum;
import com.libre.common.utils.PageUtil;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.PageVo;
import com.libre.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 部门
 * @date 2021/12/20 20:09
 **/
@Slf4j
@Transactional
@RequestMapping("/depart")
@RestController
public class DepartController extends BaseController {

    @Autowired
    private DepartService service;

    //查询带分页列表
    @GetMapping("/findPage")
    public Result<IPage<Depart>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute Depart depart) {
        IPage<Depart> page = service.findPage(PageUtil.initMpPage(pageVo), depart);
        return new ResultUtil<IPage<Depart>>().setData(page);
    }

    //查询不带分页列表
    @GetMapping("/findList")
    public Result<List<Depart>> findList(@ModelAttribute Depart depart) {
        List<Depart> list = service.findList(depart);
        return new ResultUtil<List<Depart>>().setData(list);
    }

    //通过ID查询
    @GetMapping("/findById")
    public Result<Depart> findById(String id) {
        Depart depart = service.findById(id);
        return new ResultUtil<Depart>().setData(depart);
    }

    //添加
    @PostMapping("/save")
    public Result save(@RequestBody Depart depart) {
        boolean boo = service.add(depart);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Depart depart) {
        boolean boo = service.update(depart);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //删除
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean boo = service.delete(id);
        if (boo) {
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        } else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //部门树
    @GetMapping("/departTree")
    public Result<List<Depart>> departTree(@ModelAttribute Depart depart) {
        List<Depart> departTree = service.departTree(depart);
        return new ResultUtil<List<Depart>>().setData(departTree);
    }

}
